/**
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 * @description 
 */
var rotate = function (matrix) {

    let x1 = 0, y1 = 0;//选择要旋转的第一个元素  行/列
    let times = ~~(matrix.length / 2);
    while (x1 < times ) {
        // console.log(x1, y1)
        let x2 = y1, y2 = matrix.length - 1 - x1;
        // 找到需要的的旋转元素  然后连续旋转3次
        for (let i = 0; i < 3; i++) {
            matrix[x1][y1] = matrix[x1][y1] ^ matrix[x2][y2];
            matrix[x2][y2] = matrix[x1][y1] ^ matrix[x2][y2];
            matrix[x1][y1] = matrix[x1][y1] ^ matrix[x2][y2];

            //计算下一个交换值
            let temp = y2;
            y2 = matrix.length - 1 - x2;
            x2 = temp;
        }

        if (++y1 < matrix.length - 1 - x1) {
            // 
        } else {
            x1++;
            y1=x1;
        }
    }
};

let matrix = [
    [5, 1, 9, 11],
    [2, 4, 8, 10],
    [13, 3, 6, 7],
    [15, 14, 12, 16]
]

rotate(matrix);
console.log(matrix);

